package com.wind.util;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.region.Region;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.util.Date;


/**
 * 对象存储
 *
 * @author JasonTsangLai
 * @return Map
 * @date 2019-06-19
 */
@Component
public class COSUtils {

    private static String accessKeyId;

    private static String accessKeySecret;

    private static String endpoint;

    private static String bucketName;


    @Value("${cos.accessKeyId}")
    public void setAccessKeyId(String accessKeyId) {
        this.accessKeyId = accessKeyId;
    }

    @Value("${cos.accessKeySecret}")
    public void setAccessKeySecret(String accessKeySecret) {
        this.accessKeySecret = accessKeySecret;
    }

    @Value("${cos.endpoint}")
    public void setEndpoint(String endpoint) {
        this.endpoint = endpoint;
    }

    @Value("${cos.bucketName}")
    public void setBucketName(String bucketName) {
        this.bucketName = bucketName;
    }

    /**
     * oss文件上传
     *
     * @param is       上传的文件流
     * @param dir      上传的目录
     * @param filename 文件名称
     * @return 回调路径
     */

    public static String uploadFile(File file, String dir, String filename) {
//        ClientConfiguration conf = new ClientConfiguration();
//        conf.setConnectionTimeout(5000);
//        conf.setMaxErrorRetry(10);
//        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret, conf);
//        ossClient.putObject(bucketName, dir + filename, is, null);
//        ossClient.shutdown();
//        Date expiration = new Date(System.currentTimeMillis() + 3600L * 1000 * 24 * 365 * 10);
//        URL url = ossClient.generatePresignedUrl(bucketName, dir + filename, expiration);
//        return url.toString();


        COSCredentials cred = new BasicCOSCredentials("SecretId","SecretKey");
        // 2 设置bucket的区域, COS地域的简称请参照
        // https://cloud.tencent.com/document/product/436/6224
        ClientConfig clientConfig = new ClientConfig(new Region("ap-guangzhou"));
        // 3 生成cos客户端
        COSClient cosClient = new COSClient(cred, clientConfig);
        String bucketName = "bucketName"+"-APPID";
        String key = "image/"+new Date().getTime() + ".png";
        // 简单文件上传, 最大支持 5 GB, 适用于小文件上传, 建议 20 M 以下的文件使用该接口
        // 大文件上传请参照 API 文档高级 API 上传
        // 指定要上传到 COS 上的路径
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, file);
        cosClient.putObject(putObjectRequest);
        cosClient.shutdown();
        Date expiration = new Date(new Date().getTime() + 5 * 60 * 10000);
        URL url = cosClient.generatePresignedUrl(bucketName, key, expiration);
        return url.toString();
    }
}
